package fy.hr.reinstatement;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import fy.config.FyConfig;
import fy.hr.bean.DepBean;
import fy.hr.bean.JobBean;
import fy.hr.bean.OptionBean;
import fy.util.DBUtil;


class DepAndPostDao {

	private static Log log = LogFactory.getLog(DepAndPostDao.class);

	private static final String queryDepByCorp = "select d.pk_deptdoc, d.deptname from bd_deptdoc d where d.pk_corp = ? and d.pk_fathedept is null and d.dr = 0";

	private static final String queryPostByDep = "select j.pk_jobdoc, j.jobname, d.docname from om_job j left join bd_defdoc d on j.jobrank = d.pk_defdoc where j.pk_deptdoc = ? and j.dr = 0 and j.abortdate is null";

	private static final String queryChildDepByDep = "select d.pk_deptdoc, d.deptname from bd_deptdoc d where d.pk_fathedept = ? and d.dr = 0";
	
	private DepBean depBean = new DepBean();

	private String errorMsg;

	public String getErrorMsg() {
		return errorMsg;
	}

	public DepBean getDepBean() {
		return depBean;
	}

	public boolean queryDepByCorp(String pk_corp) {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;

		boolean success = false;

		try {
			conn = DBUtil.connect(FyConfig.getEHR_DB_CONFIG());


			pst = conn.prepareStatement(queryDepByCorp);
			pst.setString(1, pk_corp);

			rs = pst.executeQuery();
			List<OptionBean> list = new ArrayList<OptionBean>();
			while (rs.next()) {
				OptionBean bean = new OptionBean();

				bean.setId(rs.getString(1));
				bean.setName(rs.getString(2));

				list.add(bean);
			}
			depBean.setChildren(list);
			success = true;

		} catch (Exception e) {
			errorMsg = "查询制造部部门列表失败";
			log.error(errorMsg, e);
			System.out.println(errorMsg);
			e.printStackTrace();
		} finally {
			DBUtil.close(rs);
			DBUtil.close(pst);
			DBUtil.close(conn);
		}
		return success;
	}

	public boolean queryPostsByDep(String pk_dept) {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;

		boolean success = false;

		try {
			conn = DBUtil.connect(FyConfig.getEHR_DB_CONFIG());


			pst = conn.prepareStatement(queryPostByDep);
			pst.setString(1, pk_dept);

			rs = pst.executeQuery();
			List<JobBean> list = new ArrayList<JobBean>();
			while (rs.next()) {
				JobBean bean = new JobBean();

				bean.setId(rs.getString(1));
				bean.setName(rs.getString(2));
				bean.setRank(rs.getString(3));

				list.add(bean);
			}
			depBean.setPosts(list);

			success = true;
		} catch (Exception e) {
			errorMsg = "查询岗位列表失败";
			log.error(errorMsg, e);
			System.out.println(errorMsg);
			e.printStackTrace();
		} finally {
			DBUtil.close(rs);
			DBUtil.close(pst);
			DBUtil.close(conn);
		}
		return success;
	}

	public boolean queryPostAndChildDepByDep(String pk_dept) {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;

		boolean success = false;

		try {
			conn = DBUtil.connect(FyConfig.getEHR_DB_CONFIG());


			pst = conn.prepareStatement(queryPostByDep);
			pst.setString(1, pk_dept);

			rs = pst.executeQuery();
			List<JobBean> list = new ArrayList<JobBean>();
			while (rs.next()) {
				JobBean bean = new JobBean();

				bean.setId(rs.getString(1));
				bean.setName(rs.getString(2));
				bean.setRank(rs.getString(3));

				list.add(bean);
			}
			depBean.setPosts(list);
			
			DBUtil.close(rs, pst);
			
			pst = conn.prepareStatement(queryChildDepByDep);
			pst.setString(1, pk_dept);

			rs = pst.executeQuery();
			List<OptionBean> depList = new ArrayList<OptionBean>();
			while (rs.next()) {
				OptionBean bean = new OptionBean();

				bean.setId(rs.getString(1));
				bean.setName(rs.getString(2));

				depList.add(bean);
			}
			depBean.setChildren(depList);
			success = true;
		} catch (Exception e) {
			errorMsg = "查询岗位和子部门列表失败";
			log.error(errorMsg, e);
			System.out.println(errorMsg);
			e.printStackTrace();
		} finally {
			DBUtil.close(rs);
			DBUtil.close(pst);
			DBUtil.close(conn);
		}
		return success;
	}
}
